package com.yjkf.zh.admin.service.impl;

import com.yjkf.zh.admin.dao.SysProductMapper;
import com.yjkf.zh.admin.model.SysProduct;
import com.yjkf.zh.admin.service.SysProductService;
import com.yjkf.zh.core.page.MybatisPageHelper;
import com.yjkf.zh.core.page.PageRequest;
import com.yjkf.zh.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysProductServiceImpl implements SysProductService {

    @Autowired
    private SysProductMapper sysProductMapper;

   // @CachePut(value = "product")
    @Override
    public int save(SysProduct record) {
        if(record.getId() == null || record.getId() == 0) {
            //添加
            sysProductMapper.insertProduct(record);
        }else{
            //修改
            sysProductMapper.updateProduct(record);
        }
        return 1;
    }

    @Override
    public int delete(SysProduct record) {
        return sysProductMapper.deleteProduct(record.getId());
    }

    @Override
    public int delete(List<SysProduct> records) {
        for(SysProduct sysProduct:records){
            delete(sysProduct);
        }
        return 1;
    }

    @Override
    public SysProduct findById(Long id) {
        return null;
    }

   // @Cacheable(value = "product")
    @Override
    public PageResult findPage(PageRequest pageRequest) {
        PageResult pageResult = null;
        Object productName = pageRequest.getParam("productName");
        if(productName != null){
            pageResult = MybatisPageHelper.findPage(pageRequest,sysProductMapper,"findPageByName",productName);
        }else{
            pageResult=MybatisPageHelper.findPage(pageRequest,sysProductMapper);
        }
        return pageResult;
    }

    @Override
    public List<SysProduct> findAll() {
        return sysProductMapper.findPage();
    }
}
